<?php

namespace App\Admin\Controllers;

use App\Models\Banner;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class BannersController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('banner列表');
            //$content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Detail');
            $content->description('description');

            $content->body(Admin::show(Banner::findOrFail($id), function (Show $show) {

                $show->id();

                $show->created_at();
                $show->updated_at();
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('修改banner');
            //$content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    public function update($id)
    {
        return $this->form()->update($id);
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加banner');
            //$content->description('description');

            $content->body($this->form());
        });
    }

    public function store()
    {
        return $this->form()->store();
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Banner::class, function (Grid $grid) {

            //$grid->id('ID')->sortable();
            $grid->image('图片')->image();
            $grid->url('URL');
            $grid->type('类型')->display(function ($value) {
                $str = '';
                switch ($value)
                {
                    case 1:
                        $str = '首页顶部';
                        break;
                    case 2:
                        $str = '用户中心非会员';
                        break;
                    case 3:
                        $str = '用户中心会员';
                        break;
                    case 4:
                        $str = '首页非会员';
                        break;
                }
                return $str;
            });
            $grid->created_at('添加时间');
            //$grid->updated_at();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->disableFilter();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Banner::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('url', 'URL');
            $form->image('image', '封面图片')->rules('required|image')->uniqueName();
            /*$form->image('image','封面图片')->name(function ($file) {
                return getPicUniqueName().'.'.$file->guessExtension();
            })->removable()->rules('required|image');*/

            $form->radio('type','类型')->options(['1' => '首页顶部', '2'=> '用户中心非会员', '3' => '用户中心会员', '4'=>'首页非会员'])->default('1');
            //$form->display('created_at', 'Created At');
            //$form->display('updated_at', 'Updated At');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
                $tools->disableDelete();
            });
        });
    }
}
